package com.srwl.mytx.widget;

import android.content.Context;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.IntRange;

import com.github.florent37.viewanimator.ViewAnimator;
import com.srwl.mytx.R;

public class BottomBar extends LinearLayout implements View.OnClickListener {
    private Context context;
    private OnTabSelectListener onTabSelectListener;
    private LinearLayout ll_world;
    private LinearLayout ll_conversation;
    private LinearLayout ll_live;
    private LinearLayout ll_profile;
    private ImageView iv_earth;
    private ImageView iv_conversation;
    private ImageView iv_live;
    private ImageView iv_profile;
    private TextView tv_earth;
    private TextView tv_conversation;
    private TextView tv_live;
    private TextView tv_profile;
    private View currentSelectItem;
    private BottomBarBadge tv_unread;


    public BottomBar(Context context) {
        super(context);
    }

    public BottomBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        init();
    }

    public BottomBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init();
    }

    public BottomBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
        init();
    }

    private void init() {
        inflate(context, R.layout.bottom_bar_layout, this);
        ll_world = findViewById(R.id.ll_world);
        ll_conversation = findViewById(R.id.ll_conversation);
        ll_live = findViewById(R.id.ll_live);
        ll_profile = findViewById(R.id.ll_profile);
        iv_earth = findViewById(R.id.iv_earth);
        iv_conversation = findViewById(R.id.iv_conversation);
        iv_live = findViewById(R.id.iv_live);
        iv_profile = findViewById(R.id.iv_profile);
        tv_earth = findViewById(R.id.tv_earth);
        tv_conversation = findViewById(R.id.tv_conversation);
        tv_live = findViewById(R.id.tv_live);
        tv_profile = findViewById(R.id.tv_profile);
        tv_unread = findViewById(R.id.tv_unread);
        ll_world.setOnClickListener(this);
        ll_conversation.setOnClickListener(this);
        ll_live.setOnClickListener(this);
        ll_profile.setOnClickListener(this);
    }

    public void setOnTabSelectListener(OnTabSelectListener onTabSelectListener) {
        this.onTabSelectListener = onTabSelectListener;
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (currentSelectItem != null) {
            int id1 = currentSelectItem.getId();
            if (id1 != id) {
                resetColor(id1, getResources().getColor(R.color.black));
                // currentSelectItem.startAnimation(new ScaleAnimation(1.0f, 0.95f, 1.0f, 0.95f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
                ViewAnimator.animate(currentSelectItem).scale(1.1f, 1).duration(200).start();
                if (id1 == R.id.ll_conversation && tv_unread.getCount() != 0) {
                    tv_unread.show();
                }
            } else {//重复按同一个 选项卡的时候，直接返回，不做操作
                return;
            }
        }
        currentSelectItem = view;
        resetColor(id, getResources().getColor(R.color.btn_blue_normal));
        //view.startAnimation( new ScaleAnimation(0.95f, 1.0f, 0.95f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
        ViewAnimator.animate(view).scale(1, 1.1f).duration(200).start();
        if (id == R.id.ll_conversation && tv_unread.getCount() != 0) {
            tv_unread.hide();
        }
        if (onTabSelectListener != null) {
            onTabSelectListener.onTabSelected(id);
        }
    }


    private void resetColor(int id, int color) {
        switch (id) {
            case R.id.ll_world:
                iv_earth.setColorFilter(color);
                tv_earth.setTextColor(color);
                break;

            case R.id.ll_conversation:
                iv_conversation.setColorFilter(color);
                tv_conversation.setTextColor(color);

                break;

            case R.id.ll_live:

                iv_live.setColorFilter(color);
                tv_live.setTextColor(color);

                break;

            case R.id.ll_profile:

                iv_profile.setColorFilter(color);
                tv_profile.setTextColor(color);
        }
    }

    public void setBadgeCount(int totalUnReadCount) {
        tv_unread.setCount(totalUnReadCount);
        //如果当前选项卡不是选中会话选项卡，需要让未读数小红点显示出来
        if (currentSelectItem != null) {
            if (currentSelectItem.getId() != R.id.ll_conversation && !tv_unread.isVisible()) {
                tv_unread.show();
            }
        } else {
            if (!tv_unread.isVisible()) {
                tv_unread.show();
            }
        }
    }

    public void removeBadge() {
        tv_unread.setCount(0);
        tv_unread.hide();

    }

    public interface OnTabSelectListener {
        void onTabSelected(int tabId);
    }


    private ShapeDrawable make(@IntRange(from = 0) int size, @ColorInt int color) {
        ShapeDrawable indicator = new ShapeDrawable(new OvalShape());
        indicator.setIntrinsicWidth(size);
        indicator.setIntrinsicHeight(size);
        indicator.getPaint().setColor(color);
        return indicator;
    }
}
